
export const RE_COLOR_HEX = /^#[0-9A-Fa-f]{6}$/
export const RE_COLOR_RGB = /^rgb\(/
export const RE_COLOR_RGBA = /^rgba\(/

    
export const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
  arr.slice(i * size, (i * size) + size)
)
export const rgbaTransfer = (color, opacity) => {
  let rgb
  if (RE_COLOR_RGB.test(color)) {
    rgb = color
  } else if (RE_COLOR_RGBA.test(color)) {
    const opacityToReplace = color.split(',')[3].slice(0, -2)
    return color.replace(opacityToReplace, opacityToReplace * opacity)
  } else if (RE_COLOR_HEX.test(color)) {
    // rgb = `rgb(${chunk(color.substr(1), 2).map(item => Number(`0x${item.join()}`)).join(', ')})`
    rgb = `rgb(${chunk(color.substr(1), 2).map(item => Number(`0x${item}`)).join(', ')})`
  } else {
    return color
  }
  const arr = rgb.split('')
  arr.splice(-1, 0, `, ${opacity}`)
  return arr.join('').replace('rgb', 'rgba')
}